package com.sk.sourcecircle.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.sk.sourcecircle.app.MyApplication;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by sk on 2017/2/20.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = MyApplication.getInstance().getIwxapi();
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.v("onResp",resp.errStr +"\n" + resp.errCode+ "\n" + resp.openId +"\n" +resp.getType() + "\n" +resp.transaction);
        String result = "";
        switch(resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = "支付成功";
                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
                EventBus.getDefault().post("pay");
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = "取消支付";
                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
                EventBus.getDefault().post("pay");
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = "发送被拒绝";
                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
                EventBus.getDefault().post("pay");
                finish();
                break;
            default:
                result = "发送返回";
                Toast.makeText(this, result, Toast.LENGTH_LONG).show();
                EventBus.getDefault().post("pay");
                finish();
                break;
        }
    }
}
